﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Hotel.WPF.HotelWCFSurIIS;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;

namespace Hotel.WPF.ui
{
    /// <summary>
    /// Interaction logic for P_Default.xaml
    /// </summary>
    public partial class P_Default : Page
    {
        HotelClient srvService;
        public P_Default()
        {
            InitializeComponent();
        }

        private void loadChambre()
        {
            #region etablissement connexion avec serveur

            try
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate { busyTool.IsBusy = true;
                    wrapPanel1.Children.Clear();
                    },
                    null
                    );
                var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
                backgroundWorker.DoWork += (s, args) =>
                {
                    srvService = new HotelClient();
                    #region Creation des userscontrols correspodants aux chambres
                    var lesChambres = srvService.getListChambreHotels();

                    //ui.Chambre.UserCtrlChambre ch = new ui.Chambre.UserCtrlChambre();
                    //ui.Chambre.UserCtrlChambre ch = null;
                    //TextBlock t = null;
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (SendOrPostCallback)delegate
                    {
                        foreach (Chambre_Hotels item in lesChambres)
                        {
                            ui.Chambre.UserCtrlChambre ch = new ui.Chambre.UserCtrlChambre(item);
                            //TextBlock t = new TextBlock();
                            //t.Width = ch.Width;
                            //t.Height = ch.Height;
                            //t.Margin = new Thickness(0);
                            //t.Inlines.Add("Chambre : " + item.Numero_Chambre);
                            //t.Inlines.Add(new LineBreak());
                            //t.Inlines.Add("Statut : " + item.Statut_ID);
                            //t.Inlines.Add(new LineBreak());
                            //t.Inlines.Add("Etage : " + item.Nombre_Etage_Chambre);
                            //ch.button1.Content = t;
                            ch.Margin = new Thickness(2);
                            wrapPanel1.Children.Add(ch);
                        }
                    },
                    null
                    );
                    
                    #endregion
                };

                backgroundWorker.ProgressChanged += (s, args) => writeToBusyTool("");
                backgroundWorker.RunWorkerCompleted += (s, args) => Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                                    (SendOrPostCallback)
                                                                                    delegate
                                                                                    {
                                                                                        busyTool.IsBusy = false;
                                                                                    },
                                                                                    null);
                backgroundWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            #endregion
        }

        private void writeToBusyTool(string message)
        {
            Dispatcher.BeginInvoke(
                DispatcherPriority.Normal,
                (SendOrPostCallback)delegate
                {
                    busyTool.BusyContent = message;
                },
                null
                );
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            loadChambre();
        }
    }
}
